package com.uenpay.im.ui.old.activity.publicnumber;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qyx.android.message.type.OperationType;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-8 上午10:27:35
 * @copyRight http://www.qiyunxin.com
 * @function 我的公众号列表
 */
public class MyPublicNumberListActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private PublicNumberAdapter numberAdapter = null;
    private View loading;
    private int page = 1;
    private TextView no_data_tv;
    private RefreshListView listView;
    private MsgReceiver msgReceiver;
    private PublicNumHandle mPublicNumHandle = new PublicNumHandle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_xlist_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        loading.setVisibility(View.VISIBLE);
        getData();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.public_number));
        listView = (RefreshListView) findViewById(R.id.discover_listview);
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.search));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        loading = findViewById(R.id.loading);
        no_data_tv = (TextView) findViewById(R.id.no_data_tv);
    }

    protected void initListener() {
        listView.setPullLoadEnable(true);
        listView.setRefreshListViewListener(this);
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        ActivityHandle.startActivity(
                                MyPublicNumberListActivity.this,
                                SearchPublicNumActivity.class, null, -1);
                    }
                });

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                PublicNumEntity numEntity = (PublicNumEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (numEntity != null) {
                    /** 公众号信息OperationType==4 **/
                    ActivityHandle.startChat(MyPublicNumberListActivity.this,
                            Long.valueOf(numEntity.custid),
                            PacketType.PERSONAL, OperationType.PUBLIC_NUM,
                            numEntity.publicNumName, 0, null);
                    finish();
                }
            }
        });
        //返回键点击
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    private void getData() {
        mPublicNumHandle.getPublicList(page, new PublicNumHandle.IGetPublicListListener() {

            @Override
            public void getPublicList(int status, String msg,
                                      ArrayList<PublicNumEntity> arrayList) {
                myHandler.sendEmptyMessage(0);
                onLoad();
                if (arrayList == null && status == -1) {
                    return;
                }
                if (status == 0) {
                    if (page == 1) {
                        if (arrayList.size() < 1) {
                            no_data_tv.setVisibility(View.VISIBLE);
                            listView.setVisibility(View.GONE);
                        } else if (numberAdapter == null) {
                            numberAdapter = new PublicNumberAdapter(
                                    MyPublicNumberListActivity.this, arrayList);
                            listView.setAdapter(numberAdapter);
                        } else {
                            numberAdapter.setData(arrayList);
                        }
                        listView.setPullLoadEnable(true);
                    } else {
                        if (arrayList != null && arrayList.size() > 0) {
                            numberAdapter.addData(arrayList);
                        } else {
                            listView.setPullLoadEnable(false);
                        }
                    }
                    no_data_tv.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);

                } else {
                    if (page == 1) {
                        no_data_tv.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                    } else {
                        listView.setPullLoadEnable(false);
                    }
                }
            }
        });
    }

    public Handler myHandler = new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message arg0) {
            if (arg0.what == 0) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            }
            return false;
        }
    });

    @Override
    protected void onResume() {
        if (msgReceiver == null) {
            msgReceiver = new MsgReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(BroadcastAction.REFRESH_PUBLIC_LIST_ACTION);
            registerReceiver(msgReceiver, intentFilter);
        }
        super.onResume();
    }

    private class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (arg1.getAction().equals(
                    BroadcastAction.REFRESH_PUBLIC_LIST_ACTION)) {
                page = 1;
                getData();
            }
        }

    }

    @Override
    protected void onDestroy() {
        if (msgReceiver != null) {
            unregisterReceiver(msgReceiver);
        }
        IMApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }

    private void onLoad() {
        listView.stopRefresh();
        listView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }
}
